﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using SPRepository;
using System.Diagnostics;

namespace SPRepositoryPerformanceTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //var siteUrl = "http://posdev.sp.local";
            //var listName = "Document Templates";
            var siteUrl = "http://pos.sp.local";
            var listName = "Test";

            using (var site = new SPSite(siteUrl))
            {
                using (var web = site.OpenWeb())
                {
                    var list = web.Lists[listName];

                    // prime the DB
                    foreach (SPListItem item in list.Items)
                    {
                        var somework = item.ID;
                    }

                    TestAnnouncementContentType(list);
                }
            }
        }

        public static void TestItemContentType(SPList list)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            foreach (SPListItem item in list.Items)
            {
                Console.Out.WriteLine(item.ID);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Title]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Modified]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Author]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Created]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Editor]);
            }
            stopwatch.Stop();

            Console.Out.WriteLine("Iterating list took {0} ms", stopwatch.ElapsedMilliseconds);

            var listRepository = new SPGenericRepository<ItemContentType, ItemContentTypeDto>(list);

            stopwatch = new Stopwatch();
            stopwatch.Start();
            var items = listRepository.GetAll();
            foreach (var item in items)
            {
                Console.Out.WriteLine(item.Id);
                Console.Out.WriteLine(item.Title);
                Console.Out.WriteLine(item.Modified);
                Console.Out.WriteLine(item.ModifiedBy.User.LoginName);
                Console.Out.WriteLine(item.Created);
                Console.Out.WriteLine(item.CreatedBy.User.LoginName);
            }
            stopwatch.Stop();

            Console.Out.WriteLine("Repository took {0} ms", stopwatch.ElapsedMilliseconds);
            Console.In.ReadLine();
        }

        public static void TestAnnouncementContentType(SPList list)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            foreach (SPListItem item in list.Items)
            {
                Console.Out.WriteLine(item.ID);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Title]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Modified]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Author]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Created]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Editor]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Body]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Expires]);
            }
            stopwatch.Stop();

            Console.Out.WriteLine("Iterating list took {0} ms", stopwatch.ElapsedMilliseconds);

            var listRepository = new SPGenericRepository<AnnouncementContentType, AnnouncementContentTypeDto>(list);

            stopwatch = new Stopwatch();
            stopwatch.Start();
            var items = listRepository.GetAll();
            foreach (var item in items)
            {
                Console.Out.WriteLine(item.Id);
                Console.Out.WriteLine(item.Title);
                Console.Out.WriteLine(item.Modified);
                Console.Out.WriteLine(item.ModifiedBy.User.LoginName);
                Console.Out.WriteLine(item.Created);
                Console.Out.WriteLine(item.CreatedBy.User.LoginName);
                Console.Out.WriteLine(item.Body.Text);
                Console.Out.WriteLine(item.Expires);
            }
            stopwatch.Stop();

            Console.Out.WriteLine("Repository took {0} ms", stopwatch.ElapsedMilliseconds);
            Console.In.ReadLine();
        }

        public static void TestDocumentContentType(SPList list)
        {
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            foreach (SPListItem item in list.Items)
            {
                Console.Out.WriteLine(item.ID);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Title]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Modified]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Author]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Created_x0020_By]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Created]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Editor]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.Modified_x0020_By]);
                Console.Out.WriteLine(item[SPBuiltInFieldId.FileLeafRef]);
            }
            stopwatch.Stop();

            Console.Out.WriteLine("Iterating list took {0} ms", stopwatch.ElapsedMilliseconds);

            var listRepository = new SPGenericRepository<DocumentContentType, DocumentContentTypeDto>(list);

            stopwatch = new Stopwatch();
            stopwatch.Start();
            var items = listRepository.GetAll();
            foreach (var item in items)
            {
                Console.Out.WriteLine(item.Id);
                Console.Out.WriteLine(item.Title);
                Console.Out.WriteLine(item.Modified);
                Console.Out.WriteLine(item.ModifiedBy.User.LoginName);
                Console.Out.WriteLine(item.DocumentModifiedBy);
                Console.Out.WriteLine(item.Created);
                Console.Out.WriteLine(item.CreatedBy.User.LoginName);
                Console.Out.WriteLine(item.DocumentCreatedBy);
                Console.Out.WriteLine(item.FileName);
            }
            stopwatch.Stop();

            Console.Out.WriteLine("Repository took {0} ms", stopwatch.ElapsedMilliseconds);
            Console.In.ReadLine();
        }
    }
}
